package service;

import com.sun.org.apache.bcel.internal.generic.RET;
import dao.BedDao;
import dao.BedDaoImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pojo.Bed;
import pojo.CheckInRecord;
import utils.JDBCUtils;
import utils.SystemEnum;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;

public class BedServiceImpl implements BedService{
    private BedDao dao = new BedDaoImpl();
    private Connection con = null;
    private Log log = LogFactory.getLog(getClass());

    @Override
    public ArrayList<Bed> findAll() {
        return dao.findAll();
    }

    @Override
    public int findCanLiveTotal() {
        ArrayList<Bed> list = dao.findAll();
        ArrayList<Bed> beds = new ArrayList<Bed>();
        for (Bed bed : list) {
            if((!bed.isUsed())){
                beds.add(bed);
            }
        }
        return beds.size();
    }

    @Override
    public Bed findById(Integer roomId, Integer bedId) {
        return dao.findById(roomId, bedId);
    }

    @Override
    public boolean findByIdReturnBool(Integer roomId, Integer bedId) {
        return dao.findByIdReturnBool(roomId, bedId);
    }

    @Override
    public boolean isEmpty(Integer roomId, Integer bedId) {
        return dao.isEmpty(roomId, bedId);
    }

    @Override
    public int insert(Bed bed){
        log.info(SystemEnum.BEGIN);
        PreparedStatement statement2 = null;
        PreparedStatement statement3 = null;
                System.out.println(bed);
        try {
            con = JDBCUtils.getConnection();
            con.setAutoCommit(false);

            int row1 = dao.insert(con,bed);
            //更新房间内床数量
            String sql2 = "UPDATE dorm SET dormSize = dormSize+1 WHERE dormID = ?";
            statement2 = con.prepareStatement(sql2);
            statement2.setInt(1, bed.getDormId());
            int row2 = statement2.executeUpdate();
            int row3 = 0;
            //更新房价内空床数量
            if(!bed.isUsed()) {
                //无人使用
                String sql3 = "UPDATE dorm SET dormEmpty = dormEmpty+1 WHERE dormID = ?";
                statement3 = con.prepareStatement(sql3);
                statement3.setInt(1, bed.getDormId());
                row3 = statement3.executeUpdate();
            }
            con.commit();
            log.info(SystemEnum.SUCCESS);
            return row1+row2+row3;
        } catch (SQLException e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE+"\t正在回滚");
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            try {
                con.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            log.info(SystemEnum.END);
        }
        return 0;
    }

    @Override
    public int update(Bed bed) {
        log.info(SystemEnum.BEGIN);
        try {
            con =  JDBCUtils.getConnection();
            con.setAutoCommit(false);
            //床铺状态装换
            int row = dao.update(con,bed);
            //房间信息修改

            PreparedStatement statement2 = null;
            //更新dorm表中空床位的信息
            String sql2 = "UPDATE dorm SET dormEmpty = dormEmpty-1 WHERE dormID = ?";
            String sql3 = "UPDATE dorm SET dormEmpty = dormEmpty+1 WHERE dormID = ?";
            if(bed.isUsed()){
                //原：有人使用    现：无人使用
                statement2 = con.prepareStatement(sql3);
            }else statement2 = con.prepareStatement(sql2);
            statement2.setInt(1, bed.getDormId());
            int row2 = statement2.executeUpdate();

            log.info(SystemEnum.SUCCESS);
            con.commit();
            return row+row2;

        }catch (Exception e){
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE+"\t正在回滚");
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            try {
                con.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            JDBCUtils.close(con,null);
        }
        return 0;
    }

    @Override
    public int delete(Bed bed) {
        try {
            con = JDBCUtils.getConnection();
            con.setAutoCommit(false);

            //删除床
            int row1 = dao.delete(con,bed);

            //dorm表更新
            PreparedStatement statement2 = null;
            //如果该床之前无人使用则房间中空床位-1 否则而不变，该房间大小-1
            String sql2 = "UPDATE dorm SET dormSize = dormSize-1,dormEmpty = dormEmpty -1 WHERE dormID = ?";
            String sql3 = "UPDATE dorm SET dormSize = dormSize-1 WHERE dormID = ?";
            if(bed.isUsed()){
                //true 有人使用
                statement2 = con.prepareStatement(sql3);
            }else{
                //无人使用
                statement2 = con.prepareStatement(sql2);
            }
            statement2.setInt(1, bed.getDormId());
            int row2=statement2.executeUpdate();
            log.info("更新dorm表"+SystemEnum.SUCCESS);
            con.commit();
            return row1+row2;
        } catch (SQLException e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE+"\t正在回滚");
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            try {
                con.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            JDBCUtils.close(con,null);
        }

        return 0;
    }
}
